<?php defined('SYSPATH') or die('Restricted Access');
function pdfGetHeight($path)
{
	$output=array();
	exec("pdfinfo $path",$output);
	foreach($output as $line)
	{
		if( strpos($line,"Page size:")===0 )
		{
			$str=substr($line,4+4+2,strlen($line)-(4+4+2));
			$words=explode(' ',$str);

			for($i=0;$i<count($words);$i++)
			{
				if( intval($words[$i])!==0 )
				{
					return $words[$i+2];
				}
			}
		}

	}

	return false;
}
function pdfGetWidth($path)
{
	$output=array();
	exec("pdfinfo $path",$output);
	foreach($output as $line)
	{
		if( strpos($line,"Page size:")===0 )
		{
			$str=substr($line,4+4+2,strlen($line)-(4+4+2));
			$words=explode(' ',$str);

			for($i=0;$i<count($words);$i++)
			{
				if( intval($words[$i])!==0 )
				{
					return $words[$i];
				}
			}
		}

	}

	return false;
}
function pdfGetPageNum($path)
{
	$output=array();
	exec("pdfinfo $path",$output);
	foreach($output as $line)
	{
		if( strpos($line,"Pages:") ===0 )
		{
			$num=(int)substr($line,6,strlen($line)-6);
			return $num;
		}

	}

	return false;

}
class Controller_Record extends Controller_Template{
	public $template='template/record';
//	public $auto_render=true; // default true
	public $allowed_roles=array('admin','recorder','login');
	public function before()
	{
		parent::before();
			
		if($this->auto_render)
		{
			$this->template->title='';
			$this->template->content='';
			$this->template->styles=array();
			$this->template->scripts=array();
		}
                if($this->request->action()=='receive_sound')
                {
                        return;
                }
//              $this->session=Session::instance();
                $auth=Auth::instance();
                if($auth->logged_in(/*$this->allowed_roles*/)===false)
                {
                        Request::current()->redirect('user/login');
                }

	}
	public function after()
	{

		parent::after();
	}
	public function action_index()
	{
		$this->template->title=__('Upload');
		$this->template->content=View::factory('upload');

	}
	public function action_record()
	{
		$lecture=ORM::factory('lecture');
		$lecture->userid=Auth::instance()->get_user()->id;
		$lecture->save();
		// slide_loc= xx/media/slide_name  , img_folder= xx/media/slide_name/

		chdir(APPPATH .'media/');
		mkdir($lecture->id);
		
		$lecture->folder= APPPATH.'/media/'.$lecture->id."/";

		$slide_file=basename($_FILES['file']['name']);
		Upload::save($_FILES['file'], $slide_file , $lecture->folder);

		chdir($lecture->folder);
		exec("pdf2svg $slide_file %d.svg all");
	
		$lecture->slide_name=$slide_file;
		$lecture->img_num=pdfGetPageNum($lecture->folder.$lecture->slide_name);
		$truncate_file=substr($slide_file,0,strrpos($slide_file,'.'));
		$lecture->path_name="path.txt";
		$lecture->sound_name="recording.wav";
		$lecture->save();
		
		$this->template->title=__('Record lecture');
		$this->template->content=View::factory('record');

		$this->template->content->id=$lecture->id;
		$this->template->content->img_num=$lecture->img_num;
		$this->template->content->height=pdfGetHeight($lecture->folder.$lecture->slide_name);
		$this->template->content->width=pdfGetWidth($lecture->folder.$lecture->slide_name);
	}

	

	public function action_receive_points($id) // $id is lecture->id
	{
		if (isset($_POST['data'])) {
			file_put_contents( APPPATH."media/$id/path.txt",$_POST['data']);
		}
	}


	public function action_recorded()
	{

	}

	public function action_record_error()
	{

	}
	
	public function action_receive_sound($id)
	{
		
	//	file_put_contents(APPPATH."media/$id/sound.wav",,);
	file_put_contents("/tmp/record.getin","ok gets in receive_sound");
		chdir(APPPATH."media/$id/");
		$data = $GLOBALS['HTTP_RAW_POST_DATA'];
		if (is_null($data)) {
   			file_put_contents("/tmp/record.err","No data was sent");
	   	} else {
      			$file = fopen("recording.wav", "w") or die("Can't open file");
         		if(!fwrite($file, $data)){
				echo "Error writing to file";
   				file_put_contents("/tmp/record.err","No data was sent");
	        	}
			print "OK\n";
			fclose($file);
		}

	}

	
}
?>
